package com.shadow.bus.main.work.complete;

import com.shadow.bus.config.ItemFiled;
import com.shadow.bus.config.ItemType;
import com.shadow.bus.entity.PartEntity;
import com.shadow.bus.entity.PersonEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.MultipleRecyclerAdapter;
import com.tepth.shadow.ui.recycler.MultipleViewHolder;

import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * @author zhangyu
 * @date 2018/7/11
 */

public class WorkCompleteAdapter extends MultipleRecyclerAdapter {
    protected WorkCompleteAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_HEAD, R.layout.item_trouble_head);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_CONTENT, R.layout.item_trouble_content);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH, R.layout.item_trouble_operator_punch);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK, R.layout.item_trouble_operator_rework);
    }

    @Override
    protected void convert(MultipleViewHolder helper, MultipleItemEntity item) {
        switch (item.getItemType()) {
            case ItemType.TYPE_ITEM_TROUBLE_HEAD:
                helper.setText(R.id.tv_header, (CharSequence) item.getField(ItemFiled.FILED_NAME));
                break;
            case ItemType.TYPE_ITEM_TROUBLE_CONTENT:
                TroubleEntity troubleEntity = item.getField(ItemFiled.ITEM_OBJ);
                if (troubleEntity != null) {
                    if (troubleEntity.getName() != null) {
                        helper.setText(R.id.tv_name, troubleEntity.getName().replace("自定义录入@@", "附加作业项"));
                    }
                }
                helper.setVisible(R.id.tv_add, false);
                break;
            case ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH:
                TroubleOperationEntity operationPunch = item.getField(ItemFiled.ITEM_OBJ);
                String troubleName = item.getField(ItemFiled.ITEM_TROUBLE_NAME);
                if (troubleName != null) {
                    helper.setText(R.id.tv_trouble_des, troubleName.replaceAll("自定义录入@@", "附加作业项"));
                }
                if (operationPunch != null) {
                    if (operationPunch.getOperation() != null) {
                        helper.setText(R.id.tv_operator, operationPunch.getOperation().getName());
                    }
                    PartEntity partPunch = operationPunch.getAccessory() == null ? operationPunch.getComponent() : operationPunch.getAccessory();
                    if (partPunch != null) {
                        helper.setText(R.id.tv_part, partPunch.getName());
                        helper.setText(R.id.tv_count, operationPunch.getUseCount() + "");
                        helper.setText(R.id.tv_unit, partPunch.getUnit());
                    }
                    PersonEntity personEntity = operationPunch.getWorkPerson();
                    helper.setText(R.id.tv_person, personEntity == null ? "无打卡人" : personEntity.getName());
                }
                helper.setVisible(R.id.tv_cancel, false);
                break;
            case ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK:
                TroubleOperationEntity operationRework = item.getField(ItemFiled.ITEM_OBJ);
                String name = item.getField(ItemFiled.ITEM_TROUBLE_NAME);
                if (name != null) {
                    helper.setText(R.id.tv_trouble_des, name.replaceAll("自定义录入@@", "附加作业项"));
                }
                if (operationRework != null) {
                    if (operationRework.getOperation() != null) {
                        helper.setText(R.id.tv_operator, operationRework.getOperation().getName());
                    }
                    PartEntity partRework = operationRework.getAccessory() == null ? operationRework.getComponent() : operationRework.getAccessory();
                    if (partRework != null) {
                        helper.setText(R.id.tv_part, partRework.getName());
                        helper.setText(R.id.tv_count, operationRework.getUseCount() + "");
                        helper.setText(R.id.tv_unit, partRework.getUnit());
                    }
                    PersonEntity personRework = operationRework.getWorkPerson();
                    helper.setText(R.id.tv_person, personRework == null ? "无打卡人" : personRework.getName());
                }

                break;
        }
    }
}
